package edu.example.paperselection;

import java.util.Collections;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class PaperSelection {
    // 关键修复：用 ConcurrentHashMap 保证线程安全
    private final Map<Paper, Student> selections = new ConcurrentHashMap<>();

    public void accept(PaperSelectionVisitor visitor) {
        visitor.visit(this);
    }

    public boolean addSelection(Student student, Paper paper) {
        // ConcurrentHashMap 的 putIfAbsent 原子操作，避免重复选择
        return selections.putIfAbsent(paper, student) == null;
    }

    public boolean isSelected(Paper paper) {
        return selections.containsKey(paper);
    }

    public Map<Paper, Student> getSelections() {
        return Collections.unmodifiableMap(selections);
    }
}